package com.jk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo {
    public static int MaxSubSumOfSequence(Integer[] list){
        List<Integer> maxNumList = new ArrayList<>();
        int sum = 0;
        for (int i = 0; i < list.length; i++) {
            if(list[i] > 0 ){
                if(sum >= 0){
                    sum += list[i];
                }else{
                    maxNumList.add(sum-list[i-1]);
                    sum = list[i];
                }
                if(i == list.length - 1){
                    maxNumList.add(sum);
                }
            }else if(i < list.length - 1 && list[i + 1] + list[i] > 0){
                sum = sum + list[i];
            }else {
                maxNumList.add(sum);
                sum = 0;
            }
        }
        Collections.sort(maxNumList);
        return maxNumList.get(maxNumList.size()-1);
    }


    public static void main(String[] args) {
//        Integer[] list = new Integer[]{1, -2, 3, 5, -1};
        Integer[] list = new Integer[]{1, -2, 3, -8, 5, 1};
//        Integer[] list = new Integer[]{1, -2, 3,-2, 5, 1};
        System.out.println(MaxSubSumOfSequence(list));
    }
}
